package com.wanxi.news.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wanxi.common.entity.NewsModel;
import com.wanxi.common.entity.NewsTypeModel;
import com.wanxi.common.result.ResultDTO;
import com.wanxi.common.util.Tool;
import com.wanxi.feign.clients.NewstypeFeignClient;
import com.wanxi.news.mapper.NewsMapper;
import com.wanxi.news.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private NewstypeFeignClient newstypeFeignClient;

    @Override
    public ResultDTO add(NewsModel newsModel) {
        LambdaQueryWrapper<NewsModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsModel::getId)
                .eq(newsModel.getName()!=null,NewsModel::getName,newsModel.getName());
        boolean exists = newsMapper.exists(queryWrapper);
        if (exists) {
            return ResultDTO.builder()
                    .code(0)
                    .msg("same name")
                    .count(-1)
                    .data("")
                    .build();
        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(newsMapper.insert(newsModel))
                    .data("")
                    .build();
        }
    }

    @Override
    public ResultDTO update(NewsModel newsModel) {
        LambdaQueryWrapper<NewsModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsModel::getId)
                .eq(newsModel.getName()!=null,NewsModel::getName,newsModel.getName());
        boolean exists = newsMapper.exists(queryWrapper);
        if (exists) {
            return ResultDTO.builder()
                    .code(0)
                    .msg("")
                    .count(newsMapper.updateById(newsModel))
                    .data("")
                    .build();

        } else {
            return ResultDTO.builder()
                    .code(0)
                    .msg("same name")
                    .count(-1)
                    .data("")
                    .build();
        }
    }

    @Override
    public ResultDTO delete(NewsModel newsModel) {
        return ResultDTO.builder()
                .code(0)
                .msg("")
                .count(newsMapper.deleteById(newsModel))
                .data("")
                .build();
    }

    @Override
    public ResultDTO findById(NewsModel newsModel) {
        LambdaQueryWrapper<NewsModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(NewsModel::getId,NewsModel::getName,NewsModel::getContent,
                        NewsModel::getImgHref,NewsModel::getIsRecommend,NewsModel::getIsShow,
                        NewsModel::getNewsTypeId,NewsModel::getTime,NewsModel::getEnable)
                .eq(newsModel.getId()!=null,NewsModel::getId,newsModel.getId());
        NewsModel news = newsMapper.selectOne(queryWrapper);
        return ResultDTO.builder()
                .code(0)
                .msg("")
//                .count()
                .data(news)
                .build();
    }

    @Override
    public ResultDTO findAll(NewsModel newsModel) {
        int page = Tool.nullToPage(newsModel.getPage());
        int limit = Tool.nullToLimit(newsModel.getLimit());
        newsModel.setPage((page - 1) * limit);
        newsModel.setLimit(limit);

        List<NewsModel> newsModels = newsMapper.findAll(newsModel);
        int count = newsMapper.count(newsModel);

        List<NewsModel> newsModelList = new ArrayList<>();

        for (NewsModel news:newsModels) {
            try {
                Long id = news.getNewsTypeId();
                NewsTypeModel newsTypeById = newstypeFeignClient.findNewsTypeById(id);
                news.setNewsTypeName(newsTypeById.getName());
                newsModelList.add(news);
            }catch (Exception e){
                e.printStackTrace();
            }


        }

        return ResultDTO.builder()
                .code(0)
                .msg("")
                .count(count)
                .data(newsModelList)
                .build();
    }

}